/*************************************************************************
	> File Name: P1062.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-01-08 10:20:17
 ************************************************************************/
#include <iostream>
#include <stack>
#include <cmath>
using namespace std;

int main() {
    long long k, n, ans = 0;
    cin >> k >> n;
    stack<int> s;
    while (n) s.push(n & 1), n >>= 1;
    while (!s.empty()) ans += s.top() * pow(k, s.size() - 1), s.pop();
    cout << ans << endl;
    return 0;
}
